using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class miniweb_product : System.Web.UI.Page
{
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    protected long cid
    {
        get
        {
            object o = ViewState["cid"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["cid"] = value;
        }
    }
    ProductSellCollection _ProductSellCollection = new ProductSellCollection();
    protected void Page_Load(object sender, EventArgs e)
    {

        HtmlAnchor aMenuProduct = (HtmlAnchor)Page.Master.FindControl("aMenuProduct");
        aMenuProduct.Attributes.Add("class", "Select");
        if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
        {
            this.cid = long.Parse(Request.QueryString["cid"]);
            ProductCat _ProductCat = ProductCatService.GetProductCat(this.cid);
            if (_ProductCat != null)
            {
                ltrTitleCatProduct.Text = _ProductCat.Name;
            }
        }
        if (IsPostBack)
            return;
        LoadQuantityNews();
        LoadProductNew();
        LoadTitlePage();

    }
    #region Load Title Page

    private void LoadTitlePage()
    {
        if (Session["MemberID"] != null)
        {
            this.Id = long.Parse(Session["MemberID"].ToString());

            Criteria cri = new Criteria();

            cri.add(ProductSell._Table + "." + ProductSellColumns.MemberId, this.Id, Condition.EQUAL, Condition.AND);
            cri.add(ProductSell._Table + "." + ProductSellColumns.Enable, 1, Condition.EQUAL, Condition.AND);
            ProductSellCollection _ProductSellNumCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, cri.Criter);
            _ProductSellCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, cri.Criter);
            if (_ProductSellCollection.Count > 0)
            {


                string join = string.Empty;
                foreach (ProductSell _ProductSell in _ProductSellNumCollection)
                {
                    join += _ProductSell.CatId + ",";
                }
                Criteria cri_ = new Criteria();

                cri_.add(ProductCat._Table + "." + ProductCatColumns.Enable, 1, Condition.EQUAL, Condition.AND);
                string query = cri_.Criter + " And ProCateID IN(" + join.TrimEnd(',') + ")";
                ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam(ProductCatColumns.SortOrder, OrderDirection.ASC, query);
                if (_ProductCatCollection.Count > 0)
                {
                    string catTitle = string.Empty;
                    int num = 0;
                    foreach (ProductCat _ProductCat in _ProductCatCollection)
                    {
                        num = num + 1;
                        if (num != _ProductCatCollection.Count)
                            catTitle += _ProductCat.Name + ", ";
                        else
                            catTitle += _ProductCat.Name;
                    }

                    Literal ltrPageTitle = (Literal)Master.FindControl("ltrPageTitle");
                    ltrPageTitle.Text = catTitle;
                }

            }




        }
        else
            Response.Redirect("../Default.aspx");
    }
    private void LoadProductNew()
    {
        if (Session["MemberID"] != null)
        {
            this.Id = long.Parse(Session["MemberID"].ToString());

            Criteria cri = new Criteria();
            int total = 0;
            if (ddlQuantityNews.SelectedValue.Equals("0"))
            {
                hyperlinkPager.PageSize = 16;

            }
            else if (ddlQuantityNews.SelectedValue.Equals("1"))
            {
                hyperlinkPager.PageSize = 32;


            }
            else if (ddlQuantityNews.SelectedValue.Equals("2"))
            {
                hyperlinkPager.PageSize = 48;

            }

            NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
            if (this.cid != long.MinValue)
                cri.add(ProductSell._Table + "." + ProductSellColumns.CatId, this.cid, Condition.EQUAL, Condition.AND);
            cri.add(ProductSell._Table + "." + ProductSellColumns.MemberId, this.Id, Condition.EQUAL, Condition.AND);
            cri.add(ProductSell._Table + "." + ProductSellColumns.Enable, 1, Condition.EQUAL, Condition.AND);
            ProductSellCollection _ProductSellNumCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, cri.Criter);
            _ProductSellCollection = ProductSellService.GetProductSellListByParam("[" + ProductSell._Table + "].[" + ProductSellColumns.Order + "]," + "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]", OrderDirection.DESC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out total);
            if (_ProductSellCollection.Count > 0)
            {
                rptProductNew.DataSource = _ProductSellCollection;
                rptProductNew.DataBind();
                hyperlinkPager.ItemCount = total;
                if ((float)total / (float)hyperlinkPager.PageSize <= 1)
                {
                    hyperlinkPager.Visible = false;
                }
                else
                {
                    hyperlinkPager.Visible = true;
                }
                if (this.cid == long.MinValue)
                {
                    string join = string.Empty;
                    foreach (ProductSell _ProductSell in _ProductSellNumCollection)
                    {
                        join += _ProductSell.CatId + ",";
                    }
                    Criteria cri_ = new Criteria();

                    cri_.add(ProductCat._Table + "." + ProductCatColumns.Enable, 1, Condition.EQUAL, Condition.AND);
                    string query = cri_.Criter + " And ProCateID IN(" + join.TrimEnd(',') + ")";
                    ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam(ProductCatColumns.SortOrder, OrderDirection.ASC, query);
                    if (_ProductCatCollection.Count > 0)
                    {
                        string catTitle = string.Empty;
                        int num = 0;
                        foreach (ProductCat _ProductCat in _ProductCatCollection)
                        {
                            num = num + 1;
                            if (num != _ProductCatCollection.Count)
                                catTitle += _ProductCat.Name + ", ";
                            else
                                catTitle += _ProductCat.Name;
                        }

                        ltrTitleCatProduct.Text = catTitle;

                    }

                }
            }



        }
        else
            Response.Redirect("../Default.aspx");
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadProductNew();
    }
    protected void LoadQuantityNews()
    {
        this.ddlQuantityNews.Items.Add(new ListItem("16", "0"));
        this.ddlQuantityNews.Items.Add(new ListItem("32", "1"));
        this.ddlQuantityNews.Items.Add(new ListItem("48", "2"));
        this.ddlNumPro.Items.Add(new ListItem("16", "0"));
        this.ddlNumPro.Items.Add(new ListItem("32", "1"));
        this.ddlNumPro.Items.Add(new ListItem("48", "2"));
    }

    protected void ddlQuantityNews_OnSelectIndexChanged(object sender, EventArgs e)
    {
        LoadProductNew();
    }
    protected void rptProductNew_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ProductSell _ProductSell = (ProductSell)e.Item.DataItem;
            Literal ltrTitleProduct = (Literal)e.Item.FindControl("ltrTitleproductNew");
            HtmlAnchor ahrImg = (HtmlAnchor)e.Item.FindControl("ahrImg");
            HtmlAnchor ahrTitle = (HtmlAnchor)e.Item.FindControl("ahrTitle");
            HtmlImage imgProduct = (HtmlImage)e.Item.FindControl("imgProductNew");
            ahrTitle.HRef = string.Format("productDetail.aspx?id={0}", _ProductSell.ProductId);
            ahrImg.HRef = string.Format("productDetail.aspx?id={0}", _ProductSell.ProductId);
            ltrTitleProduct.Text = Utilitys.ShortSummary(23, _ProductSell.Title);

            if (!string.IsNullOrEmpty(_ProductSell.Image))
            {
                imgProduct.Src = "../photos/" + _ProductSell.Image;
            }

        }


    }

    #endregion
}
